package com.css.devops.monitor;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;
import java.util.List;

public class JenkinsCSRFDisable {
    public static void main(String[] args) throws Exception {
        // Jenkins的URL，用户名和API令牌
        String jenkinsUrl = "http://192.168.94.1:18080";
        String loginUrl = jenkinsUrl + "/j_spring_security_check";
        String crumbUrl = jenkinsUrl + "/crumbIssuer/api/json";
        String scriptUrl = jenkinsUrl + "/script";
        
        String username = "admin";
        String apiToken = "p@ssw0rd";
        
        RestTemplate restTemplate = new RestTemplate();
        
        // 登录 Jenkins 并获取 Cookie
        HttpHeaders loginHeaders = new HttpHeaders();
        loginHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        loginHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        
        MultiValueMap<String, String> loginBody = new LinkedMultiValueMap<>();
        loginBody.add("j_username", username);
        loginBody.add("j_password", apiToken);
        
        HttpEntity<MultiValueMap<String, String>> loginRequest = new HttpEntity<>(loginBody, loginHeaders);
        ResponseEntity<String> loginResponse = restTemplate.exchange(
                loginUrl,
                HttpMethod.POST,
                loginRequest,
                String.class
        );
        
        // 打印登录响应
        System.out.println("Login response status: " + loginResponse.getStatusCode());
        System.out.println("Login response body: " + loginResponse.getBody());
        
        // 获取登录后的 Cookies
        List<String> cookiesHeader = loginResponse.getHeaders().get(HttpHeaders.SET_COOKIE);
        String cookiesHeaderString = cookiesHeader != null ? String.join("; ", cookiesHeader) : "";
        System.out.println("Cookies Header: " + cookiesHeaderString);

        // 获取 Jenkins Crumb
        HttpHeaders crumbHeaders = new HttpHeaders();
        crumbHeaders.add(HttpHeaders.COOKIE, cookiesHeaderString); // 添加 Cookies
        crumbHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
        
        HttpEntity<String> crumbRequest = new HttpEntity<>(crumbHeaders);
        ResponseEntity<String> crumbResponse = restTemplate.exchange(
                crumbUrl,
                HttpMethod.GET,
                crumbRequest,
                String.class
        );
        
        ObjectMapper mapper = new ObjectMapper();
        JsonNode rootNode = mapper.readTree(crumbResponse.getBody());
        String crumb = rootNode.path("crumb").asText();
        String crumbRequestField = rootNode.path("crumbRequestField").asText();
        
        // 打印 Crumb 和 Crumb 请求字段
        System.out.println("Crumb Request Field: " + crumbRequestField);
        System.out.println("Crumb: " + crumb);
        
        // 构建请求体
        String script = "hudson.security.csrf.GlobalCrumbIssuerConfiguration.DISABLE_CSRF_PROTECTION=true";
        
        MultiValueMap<String, String> body = new LinkedMultiValueMap<>();
        body.add(crumbRequestField, crumb);
        body.add("script", script);
        
        HttpHeaders scriptHeaders = new HttpHeaders();
        scriptHeaders.add(HttpHeaders.COOKIE, cookiesHeaderString); // 添加 Cookies
        scriptHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        
        HttpEntity<MultiValueMap<String, String>> scriptRequest = new HttpEntity<>(body, scriptHeaders);
        
        ResponseEntity<String> scriptResponse = restTemplate.exchange(
                scriptUrl,
                HttpMethod.POST,
                scriptRequest,
                String.class
        );
        
        // 打印响应
        System.out.println("Script execution response status: " + scriptResponse.getStatusCode());
        System.out.println("Script execution response body: " + scriptResponse.getBody());
    }
}



